package helpers;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.select.Elements;

import models.AutoSite;
import models.News;

public class CrawlerHelper {
	
	public static ArrayList<String> getLastestNews(AutoSite autoSite) {
		ArrayList<String> listNews = new ArrayList<String>();
		try {
			Document doc = Jsoup.connect(autoSite.getSite()).get();
			Elements links = doc.select(autoSite.getRegLink());
			if (links.size() > 0) {
				for (int i = 0; i < links.size(); i++) {
					listNews.add(links.get(i).attr("href"));
				}
			}
			
		} catch (Exception e) {
			listNews = null;
			e.printStackTrace();
		}
		
		return listNews;
	}
	
	public static ArrayList<String> getPageNews(AutoSite autoSite) {
		ArrayList<String> listNews = new ArrayList<String>();
		try {
			Document doc = Jsoup.connect(autoSite.getSitePage() + autoSite.getCurrentPage()).get();
			System.out.println(autoSite.getSitePage() + autoSite.getCurrentPage());
			Elements links = doc.select(autoSite.getRegLink());
			if (links.size() > 0) {
				for (int i = 0; i < links.size(); i++) {
					listNews.add(links.get(i).attr("href"));
				}
			}
			
		} catch (Exception e) {
			listNews = null;
			e.printStackTrace();
		}
		
		return listNews;
	}
	
	public static News getNews(String url, AutoSite autoSite) {
		News news = new News();
		try {
			Document doc = Jsoup.connect(url).get();
			String title = doc.select(autoSite.getRegTitle()).html();
			if (!autoSite.getRegRemove().isEmpty()) {
				doc.select(autoSite.getRegRemove()).remove();
			}
			String description = doc.select(autoSite.getRegDescription()).html();
			//Get image storage google and read image
			Elements imgs = doc.select(autoSite.getRegDescription()).select("img");
			for (int i = 0; i < imgs.size(); i++) {
				String src = imgs.get(i).attr("src");
				String name = UrlHelper.getName(src);
				GoogleStorageHelper.getFile(src, name);
				description = description.replace(src, "http://static.1streview.org/" + name);
			}
			
			List<String> labels = new ArrayList<String>();
			labels.add(autoSite.getCategory());
			
			Boolean published = autoSite.getPublished();
			
			news.setUrl(url);
			news.setTitle(title);
			news.setDescription(description);
			news.setSource(UrlHelper.getDomainName(url));
			news.setAutoSiteId(autoSite.getId());
			news.setLabels(labels);
			news.setPublished(published);	
		} catch (IOException e) {
			news = null;
			e.printStackTrace();
		}
		
		return news;
	}
}
